﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web.Mvc;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

namespace ERPStore.Payment.Direct.Services
{
	public class PaymentDeferredService : ERPStore.Services.IPaymentService
	{
		private Models.Settings m_Settings;

        public PaymentDeferredService(ERPStore.Services.ISalesService salesService)
        {
            this.SalesService = salesService;
			var container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
			m_Settings = container.Resolve<Models.Settings>("deferredPaymentSettings");
		}


        protected ERPStore.Services.ISalesService SalesService { get; set; }

		#region IPaymentService Members

		public bool IsAllowedFor(ERPStore.Models.OrderCart cart, ERPStore.Models.UserPrincipal principal)
		{
            if (principal.CurrentUser != null
                && principal.CurrentUser.Corporate != null
                && principal.CurrentUser.Corporate.CreditScore.AllowDeferredPayment
                && !principal.CurrentUser.Corporate.CreditScore.IsLocked
                && principal.CurrentUser.Corporate.CreditScore.DefaultAllowedCredit > (principal.CurrentUser.Corporate.CreditScore.CurrentCredit + cart.GrandTotal))
            {
                return true;
            }

            return false;
		}

		#endregion

		#region IPayment Members

        public string Name
        {
            get
            {
                return "deferredpayment";
            }
        }

		public string ConfirmationViewName
		{
			get
			{
				return m_Settings.ConfirmationViewName ?? "deferredpaymentconfirmation.cshtml";
			}
		}

		public string FinalizedViewName
		{
			get
			{
				return m_Settings.FinalizedViewName ?? "deferredpaymentfinalized.cshtml";
			}
		}

		public string ConfirmationRouteName
		{
			get
			{
				return m_Settings.ConfirmationRouteName ?? "DeferredPaymentConfirmation";
			}
		}

		public string FinalizedRouteName
		{
			get
			{
				return m_Settings.FinalizedRouteName ?? "DeferredPaymentFinalized";
			}
		}

		public string GetFullDescription(ERPStore.Models.OrderCart cart, ERPStore.Models.UserPrincipal principal)
        {
            string paymentMode = null;
            string remaining = null;
            if (principal.CurrentUser != null
                && principal.CurrentUser.Corporate != null
                && !principal.CurrentUser.Corporate.CreditScore.IsLocked)
            {
                paymentMode = " " + SalesService.GetPaymentModeNameById(principal.CurrentUser.Corporate.DefaultPaymentModeId);
				remaining = string.Format("<br/>&nbsp;Encours&nbsp;:&nbsp;{0:F2}&nbsp;HT", principal.CurrentUser.Corporate.CreditScore.CurrentCredit);
            }

            return string.Format("{1}{2}", paymentMode, remaining);
        }

		#endregion
	}
}
